package cn.edu.scau.cmi.mingjunhui.composite.safe;

import java.util.Set;
import java.util.HashSet;

public class SafeComposite extends SafeComponent{
	public SafeComposite(SafeComponent org, String name) 
	{
		super(org, name);
	}
	
	private Set<SafeComponent> son = new HashSet<>();
	
	public void removeChild(SafeComposite child)
	{
		son.remove(child);
	}
	
	public void addChild(SafeComponent child)
	{
		son.add(child);
	}
	
	public Set<SafeComponent> getChild()
	{
		return son;
	}
	
	@Override
	public String toString()
	{
		return toString(0);
	}
	
	@Override
	public String toString(int pos)
	{
		String parentName = getParent() == null ? "None" : getParent().getName();
		String blank = "";
		for (int i=0; i<pos; i++) blank += " ";
		StringBuilder sb = new StringBuilder();
		for (SafeComponent s : son) {
			String rString = s.toString(pos+4);
			rString = rString.substring(0, rString.length()-1);
			String string = rString + ",\n";
			sb.append(string);
		}
		if (sb.length()>=2) {
			sb.deleteCharAt(sb.length()-2);
		}
		String rs = blank +"\"" + getName() + "\": {\n" + 
				blank + " \"类型\": \"团队\",\n" +
				blank + " \"父亲\": \"" + parentName + "\",\n" + 
				blank + " \"孩子\": [\n" + 
				sb.toString() + 
				blank + " ]\n" + 
				blank + "}\n";
		return rs;
	}

}
